package cn.jly.springboot24.config;

import cn.jly.springboot24.bean.Person;
import cn.jly.springboot24.bean.Pet;
import cn.jly.springboot24.bean.User;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;

/**
 * @PackageName cn.jly.springboot24.config
 * @ClassName BeanConfig
 * @Description
 * @Author 姬岚洋
 * @Date 2021/1/16 下午 3:52
 */
@Import({User.class}) // 给容器中注册该类型的bean，beanName默认是全类名
@Configuration(proxyBeanMethods = true) // 和@SpringBootConfiguration等价
// @SpringBootConfiguration
public class BeanConfig {

    /**
     * 外部无论对该组件的注册方法调用多少次，返回的都是同一个已经 注册的person对象
     * 关键在于 @Configuration(proxyBeanMethods = true)
     *
     * @return
     */
    @Bean
    public Person person() {
        final Person person = Person.builder()
                .id(1001L).lastName("姬岚洋").email("jly@qq.com").age(26).gender(1)
                .build();

        // 会到容器中去找对象，@Configuration(proxyBeanMethods = true)
        person.setPet(pet());

        return person;
    }

    public Pet pet() {
        return new Pet("阿黄");
    }
}
